<?php


namespace App\Models\CacheModel;


use App\Models\Api\BaseCache;
use Exception;
use think\Log;
use AstroTool\Cache\CacheManage;

class ZlcrAstrMessage extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zlcr_astr_message';

    protected $cacheModel = 'astr_admin.message';

    protected $cacheIdKey = 'id';

    /**
     * 获取占星师消息列表
     * @param int $astrId
     * @return array
     * @throws Exception
     */
    public function getMessageList(int $astrId): array
    {
        $dataList = $this->getListPage($astrId);

        if (isset($dataList['data']))
        {
            $d = [];
            foreach ($dataList['data'] as $messageItem)
            {
                $d[] = [
                    'id' => (int)$messageItem['id'],
                    'is_read' => $messageItem['is_read'] ? true : false,
                    'create_time' => (int)$messageItem['create_time'],
                    'title' => $messageItem['title'],
                    'content' => $messageItem['content'],
                    'target' => $messageItem['target'],
                    'link' => $messageItem['link']
                ];
            }
            $dataList['data'] = $d;
        }

        return $dataList;
    }

    /**
     * 消息ID
     * @param int $mid
     * @return false|int
     */
    public function messageRead(int $mid)
    {
        try
        {
            return $this->save(['is_read' => 1], ['id' => $mid]);
        }
        catch (Exception $e)
        {
            Log::write('占星师后台；消息已读失败：' . $e->getMessage(), 'error');
            return false;
        }
    }

    /**
     * 消息全部已读
     * @param int $astrId
     * @return false|int
     */
    public function messageReadAll(int $astrId)
    {
        try
        {
            $mids = $this->where('astr_id', $astrId)->column('id');

            if ($mids)
            {
                $this->save(['is_read' => 1], ['astr_id' => $astrId]);

                foreach ($mids as $mid)
                {
                    CacheManage::getInstance()->setCacheConfig($this->cacheModel, $mid)->setCache(['is_read' => 1]);
                }

                return true;
            }
            return false;
        }
        catch (Exception $e)
        {
            Log::write('占星师后台；消息已读失败：' . $e->getMessage(), 'error');
            return false;
        }
    }

    /**
     * 获取未读消息数量
     * @param int $astrId
     * @return int|string
     * @throws \think\Exception
     */
    public function getNewMessageNum(int $astrId)
    {
        return $this->where(['astr_id' => $astrId, 'is_read' => 0])->count();
    }

    /**
     * 重写数据时获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {
                $this->whereIn('id', $ids);
            } else
            {
                $this->where(['status' => 0, 'astr_id' => $type[0]]);
            }
            $data = $this->order("id {$sortType}")->column('is_read,type,astr_id,title,content,link,target,create_time,update_time', 'id');
        }
        catch (Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    /**
     * 未开启缓存从数据库获取分页数据
     * @param array $type 分页数据状态
     * @param string $sortType 排序方式
     * @return array
     */
    public function getPaginateInData($type, $sortType): array
    {
        try
        {
            $data = $this->where(['status' => 0, 'astr_id' => $type[0]])
                ->field('id,is_read,type,astr_id,title,content,link,target,create_time,update_time')
                ->order('id ' . $sortType)
                ->paginate()->toArray();
            return $data;
        }
        catch (Exception $e)
        {
            return [];
        }
    }

    /**
     * 消息中心添加
     * @param int $astrId
     * @param array $data
     * @return bool
     * @throws Exception
     */
    public function add(int $astrId, array $data)
    {
        try
        {
            $this->save($data);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $astrId)->rmCache('list');
        }
        catch (Exception $e)
        {
            Log::write("占星师「{$astrId}」发送站内信失败：" . $e->getMessage() . '[ ' . $e->getFile() . '：' . $e->getLine() . ' ]', 'error', true);
            return false;
        }
        return true;
    }
}
